package com.cosina.game.robodefence.util;


import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;

public class BitmapUtil {
	public static Bitmap[] cut(Bitmap raw, int count, int direction){
		int width = raw.getWidth();
		int height = raw.getHeight();
		int partHeight = (direction == World.V ? height/count : height);
		int partWidth = (direction == World.H ? width/count : height);
		Bitmap[] result = new Bitmap[count];
		if(direction == World.H){
			for(int i=0; i<count; i++) {
				result[i] = Bitmap.createBitmap(raw, i * partWidth, 0, partWidth, partHeight);
			}
		}else{
			for(int i=0; i<count; i++) {
				result[i] = Bitmap.createBitmap(raw, 0, i * partHeight, partWidth, partHeight);
			}
		}
		return result;
	}
	
	public static Bitmap cover(Bitmap bottom, Bitmap top){
		int picw = bottom.getWidth(), pich = bottom.getHeight();
		int[] bottomPix = new int[picw * pich];
		bottom.getPixels(bottomPix, 0, picw, 0, 0, picw, pich);
		
		int[] topPix = new int[picw * pich];
		top.getPixels(topPix, 0, picw, 0, 0, picw, pich);
		
		for (int y = 0; y < pich; y++)
			for (int x = 0; x < picw; x++) {
				int index = y * picw + x;
				
				
				int r = (topPix[index] >> 16) & 0xff;
				int g = (topPix[index] >> 8) & 0xff;
				int b = topPix[index] & 0xff;
				if(r == 0 && g == 0 && b == 0){
					bottomPix[index] = topPix[index];
				}
			}
		Bitmap bm = Bitmap.createBitmap(picw, pich, Config.ARGB_4444);
		bm.setPixels(bottomPix, 0, picw, 0, 0, picw, pich);
		return bm;
	}
	
	public static Bitmap clearBlack(Bitmap mBitmap) {
		int picw = mBitmap.getWidth(), pich = mBitmap.getHeight();
		int[] pix = new int[picw * pich];
		mBitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);
		for (int y = 0; y < pich; y++)
			for (int x = 0; x < picw; x++) {
				int index = y * picw + x;
				
				int r = (pix[index] >> 16) & 0xff;
				int g = (pix[index] >> 8) & 0xff;
				int b = pix[index] & 0xff;
				
				if(r < 50 && g < 50 && b < 50){
					pix[index] = 0x00ffffff;
				}
			}

		Bitmap bm = Bitmap.createBitmap(picw, pich, Config.ARGB_4444);
		bm.setPixels(pix, 0, picw, 0, 0, picw, pich);
		return bm;
	}
}	
